﻿using System.Linq;

namespace Hasiu.Mvc.Extensions.Pager
{
    public static class PageLinqExtensions
    {
        public static PagerList<T> ToPagerList<T>
        (
            this IQueryable<T> allItems,
            int pageIndex,
            int pageSize
        )
        {
            if (pageIndex < 1)
                pageIndex = 1;
            var itemIndex = (pageIndex - 1)*pageSize;
            var pageOfItems = allItems.Skip(itemIndex).Take(pageSize);
            var totalItemCount = allItems.Count();
            return new PagerList<T>(pageOfItems, pageIndex, pageSize, totalItemCount);
        }
    }
}
